package nl.tue.be.ds.duraark.bSDD.dump;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import nl.tue.be.ds.duraark.bSDD.DataBaseUtilities;

public class ConInRelSearchProcessor {
	
	public static final int MaxThreadNum = 4000;
	
	public Integer searchProcess(){
		Integer dumpNum = 0;
		List<Future<Integer>> futureList = new ArrayList<Future<Integer>>();
		
		HashSet<String> dumpedRelGuidSet = DataBaseUtilities.getAllValidDumpedRelGuid(DataBaseUtilities.db);
		ExecutorService threadPool = Executors.newFixedThreadPool(MaxThreadNum);
		
		Iterator<String> iter = dumpedRelGuidSet.iterator();
		while(iter.hasNext()){
			String relGuid = iter.next();
			if (!DataBaseUtilities.isConceptExistedInDB(relGuid, DataBaseUtilities.db)){
				
				ConInRelSearchPerGuidCallable cpgc = new ConInRelSearchPerGuidCallable(relGuid);	
				futureList.add(threadPool.submit(cpgc));
			}
		}
		//System.out.println(futureList.size() + " concepts in rel need to be searched and dumped");
		
		for (int i=0; i<futureList.size(); i++){
			try {
				dumpNum+= futureList.get(i).get();
			} catch (InterruptedException e) {	
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}
		threadPool.shutdown();
		return dumpNum;
	}	
	
}
